// HSS.Forms.InputDialog.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       InputDialog.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/24/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Windows.Forms;
	#endregion

	#region InputDialog
	/// <summary>
	/// Provides a Dialog Form allowing a user to enter multiple lines of text
	/// </summary>
	public partial class InputDialog : Form
	{
		#region Constructor
		/// <summary>
		/// Hide the public constructor. Can only be accessed via the Static Show Methods
		/// </summary>
		private InputDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region Properties

		/// <summary>
		/// The InputDialog's default Width
		/// </summary>
		public static int DefaultWidth
		{
			get { return new InputDialog().Width; }
		}

		/// <summary>
		/// The InputDialog's default Height
		/// </summary>
		public static int DefaultHeight
		{
			get { return new InputDialog().Height; }
		}

		/// <summary>
		/// Callback delegate to perform validation
		/// </summary>
		protected EventHandler<InputDialogValidatingArgs> Validator
		{
			get
			{
				return (this._validator);
			}
			set
			{
				this._validator = value;
			}
		} EventHandler<InputDialogValidatingArgs> _validator;
		#endregion

		#region Methods
		/// <summary>
		/// Displays a prompt in a dialog box, waits for the user to input text or click a button.
		/// </summary>
		/// <param name="prompt">String expression displayed as the message in the dialog box</param>
		/// <param name="title">String expression displayed in the Title bar of the dialog box</param>
		/// <param name="defaultResponse">String expression displayed in the text box as the default response</param>
		/// <param name="validator">Delegate used to validate the text</param>
		/// <param name="xpos">Numeric expression that specifies the distance of the left edge of the dialog box from the left edge of the screen.</param>
		/// <param name="ypos">Numeric expression that specifies the distance of the upper edge of the dialog box from the top of the screen</param>
		/// <param name="width">The desired width of the InputDialog Form</param>
		/// <returns>An InputDialogResult object with the Text and the OK property set to true when OK was clicked.</returns>
		public static InputDialogResult Show(string prompt, string title, string defaultResponse, EventHandler<InputDialogValidatingArgs> validator, int xpos, int ypos, int width)
		{
			using (InputDialog form = new InputDialog())
			{
				form.TopMost = true;
				form.labelPrompt.Text = prompt;
				form.Text = title;
				form.textBoxText.Text = defaultResponse;
				if (xpos >= 0 && ypos >= 0)
				{
					form.StartPosition = FormStartPosition.Manual;
					form.Left = xpos;
					form.Top = ypos;
					if (width > 0)
					{
						form.Width = width;
					}
				}
				else
				{
					form.StartPosition = FormStartPosition.CenterScreen;
					if (width > 0)
					{
						form.Width = width;
					}
				}
				form.Validator = validator;

				DialogResult result = form.ShowDialog();

				InputDialogResult retval = new InputDialogResult();
				if (result == DialogResult.OK)
				{
					retval.Text = form.textBoxText.Text;
					retval.OK = true;
				}
				return retval;
			}
		}
		/// <summary>
		/// Displays a prompt in a dialog box, waits for the user to input text or click a button.
		/// </summary>
		/// <param name="prompt">String expression displayed as the message in the dialog box</param>
		/// <param name="title">String expression displayed in the Title bar of the dialog box</param>
		/// <param name="defaultResponse">String expression displayed in the text box as the default response</param>
		/// <param name="validator">Delegate used to validate the text</param>
		/// <param name="width">The desired width of the InputDialog Form</param>
		/// <returns>An InputDialogResult object with the Text and the OK property set to true when OK was clicked.</returns>
		public static InputDialogResult Show(string prompt, string title, string defaultResponse, EventHandler<InputDialogValidatingArgs> validator, int width)
		{
			return Show(prompt, title, defaultResponse, validator, -1, -1, width);
		}
		/// <summary>
		/// Displays a prompt in a dialog box, waits for the user to input text or click a button.
		/// </summary>
		/// <param name="prompt">String expression displayed as the message in the dialog box</param>
		/// <param name="title">String expression displayed in the Title bar of the dialog box</param>
		/// <param name="defaultResponse">String expression displayed in the text box as the default response</param>
		/// <param name="validator">Delegate used to validate the text</param>
		/// <returns>An InputDialogResult object with the Text and the OK property set to true when OK was clicked.</returns>
		public static InputDialogResult Show(string prompt, string title, string defaultResponse, EventHandler<InputDialogValidatingArgs> validator)
		{
			return Show(prompt, title, defaultResponse, validator, -1, -1, -1);
		}
		#endregion

		#region EventHandlers

		private void textBoxText_TextChanged(object sender, EventArgs e)
		{
			this.errorProvider1.SetError(textBoxText, "");
		}

		private void textBoxText_Validating(object sender, CancelEventArgs e)
		{
			if (Validator != null)
			{
				InputDialogValidatingArgs args = new InputDialogValidatingArgs();
				args.Text = textBoxText.Text;
				Validator(this, args);
				if (args.Cancel)
				{
					e.Cancel = true;
					this.errorProvider1.SetError(textBoxText, args.Message);
				}
			}
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			this.Validator = null;
			this.Close();
		}

		#endregion
	}
	#endregion

	#region InputDialogResult
	/// <summary>
	/// Class used to store the result of an InputDialog.Show message.
	/// </summary>
	public class InputDialogResult
	{
		/// <summary>
		/// Indicate if the user selected OK or Cancel
		/// </summary>
		public bool OK;
		/// <summary>
		/// The Text entered by the user
		/// </summary>
		public string Text;
	}
	#endregion

	#region InputDialogValidatingArgs
	/// <summary>
	/// EventArgs used to Validate an InputDialog
	/// </summary>
	public class InputDialogValidatingArgs : EventArgs
	{
		/// <summary>
		/// The current text
		/// </summary>
		public string Text;
		/// <summary>
		/// The error message
		/// </summary>
		public string Message;
		/// <summary>
		/// whether or not to cancel the OK action
		/// </summary>
		public bool Cancel;
	}
	#endregion
}